#include "SelectLevelScene.h"
#include "GameSprite.h"
#include "PlayGameScene.h"

SelectLevelLayer::SelectLevelLayer()
{
}

SelectLevelLayer::~SelectLevelLayer()
{
}

bool SelectLevelLayer::init()
{
	bool result = false;
	do
	{
		CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)));
		CCSize size = CCDirector::sharedDirector()->getWinSize();

		// add background
		GameSprite* background = GameSprite::gameSpriteWithFile("background.jpg");
		CC_BREAK_IF(!background);
		background->setPosition(ccp(size.width / 2, size.height / 2));
		this->addChild(background);

		// add intro bar
		GameSprite* introBar = GameSprite::gameSpriteWithFile("IntroBar.png");
		CC_BREAK_IF(!introBar);
		introBar->setAnchorPoint(ccp(0, 1));
		introBar->setPosition(ccp(0, size.height));
		//introBar->setScale(0.8f);
		this->addChild(introBar);

		// add level list
		CCMenu *pMenu = CCMenu::create();
		CC_BREAK_IF(!pMenu);
		pMenu->setPosition(CCPointZero);
		this->addChild(pMenu, 1);

		float startX = 55;
		float startY = size.height - introBar->getContentSize().height - 125;
		
		for (int i = 0; i < LEVEL_ONE_PAGE; i++)
		{			
			CCMenuItemImage *levelButton = CCMenuItemImage::create("NormalIcon.png", "NormalIcon.png", this, menu_selector(SelectLevelLayer::levelButtonPushCallback));
			CC_BREAK_IF(!levelButton);			
			float width = (size.width - 121 - levelButton->getContentSize().width * 6) / 5 + levelButton->getContentSize().width;
			float height = (size.height - introBar->getContentSize().height - 141 - levelButton->getContentSize().height * 4) / 3 + levelButton->getContentSize().height;
			float x = startX + (i % 6) * width;
			float y = startY - (i / 6) * height;
			levelButton->setAnchorPoint(ccp(0, 0));
			levelButton->setPosition(x, y);
			pMenu->addChild(levelButton);
		}				

		result = true;
	}
	while(0);

	return result;
}

void SelectLevelLayer::levelButtonPushCallback(CCObject* pSender)
{
	PlayGameScene *pScene = PlayGameScene::create();
	CCDirector::sharedDirector()->replaceScene(pScene);
}

SelectLevelScene::~SelectLevelScene()
{

}

bool SelectLevelScene::init()
{
	bool result = false;
	do
	{
		CC_BREAK_IF(!CCScene::init());
		_layer = SelectLevelLayer::create();
		CC_BREAK_IF(!_layer);
		_layer->retain();
		this->addChild(_layer);		
		result = true;
	}
	while(0);

	return result;
}

void SelectLevelScene::gameLoop(float time)
{

}